/*
 * @Author: lineCode 1145938682@qq.com
 * @Date: 2025-02-05 22:29:33
 * @LastEditors: lineCode 1145938682@qq.com
 * @LastEditTime: 2025-02-10 15:16:18
 * @FilePath: /backend/shorthand-modules/shorthand-modules-content/src/main/java/com/angus/modules/content/service/impl/CnQuestionServiceImpl.java
 */
package com.angus.modules.content.service.impl;

import com.angus.common.core.utils.StringUtils;
import com.angus.modules.content.domain.CNCourseQuestion;
import com.angus.modules.content.domain.CNOption;
import com.angus.modules.content.dto.CnCourseQuestionDto;
import com.angus.modules.content.repository.CNOptionRepository;
import com.angus.modules.content.repository.CNQuestionRepository;
import com.angus.modules.content.service.ICnQuestionService;
import com.angus.modules.content.vo.CnCourseQuestionVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liyanan
 * @date 2024/10/28 7:17 PM
 * @description
 */
@Service
public class CnQuestionServiceImpl implements ICnQuestionService {

    @Resource
    private CNQuestionRepository questionRepository;

    @Resource
    private CNOptionRepository optionRepository;
    @Override
    public  List<CnCourseQuestionVo> getQuestion(CnCourseQuestionDto dto) {

        List<CNCourseQuestion> questionList = questionRepository.getQuestionList(dto.getCourseCode(), dto.getQuestionType(), dto.getGroupId(),dto.getUnitNum());
        List<Long> questionIds = questionList.stream().map(CNCourseQuestion::getId).collect(Collectors.toList());
        if(questionIds.isEmpty()){
            return new ArrayList<>();
        }
        List<CNOption> optionsByQuestionId = optionRepository.getOptionsByQuestionId(questionIds);
        Map<Long, List<CNOption>> cnOptionMap = optionsByQuestionId.stream().collect(Collectors.groupingBy(CNOption::getQuestionId));
        return questionList.stream().map(question -> {
            CnCourseQuestionVo  cnCourseQuestionVo = new CnCourseQuestionVo();
            String questionTitle = question.getQuestionTitle().replaceAll("[\\r\\n]+", "");
            questionTitle = questionTitle.replaceAll("。", "");
            // 如果是造句类型，则随机题目
            if (dto.getQuestionType() == 0){
                questionTitle = StringUtils.ShuffleString(questionTitle);
            }
            cnCourseQuestionVo.setQuestionTitle(questionTitle);
            cnCourseQuestionVo.setCourseCode(question.getCourseCode());
            cnCourseQuestionVo.setField1(question.getField1());
            cnCourseQuestionVo.setReadAloud(question.getReadAloud());
            cnCourseQuestionVo.setMp3Content(question.getMp3Content());
            cnCourseQuestionVo.setId(question.getId());
            cnCourseQuestionVo.setGroupId(question.getGroupId());
            cnCourseQuestionVo.setGroupFlag(question.getGroupFlag());
            cnCourseQuestionVo.setOptionType(question.getOptionType());
            List<CNOption> optionList = cnOptionMap.getOrDefault(question.getId(),new ArrayList<>());
            if (optionList.size() > 0) {
                Collections.shuffle(optionList);
            }
            cnCourseQuestionVo.setOptions(optionList);
            return cnCourseQuestionVo;
        }).collect(Collectors.toList());

    }
}
